home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / sound premixer effect / vu-meter.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  13.3 KB  |  357 lines

  1. /*
  2.     File:        VU-Meter.c
  3.  
  4.     Contains:    Sample pre-mixer sound component 
  5.  
  6.     Written by: Mark Cookson    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <Memory.h>
  25. #include <Errors.h>
  26. #include <SoundInput.h>
  27. #include <Components.h>
  28. #include <Gestalt.h>
  29. #include <Sound.h>
  30.  
  31. #include "VU-Meter.h"
  32.  
  33.  
  34. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. // Constants
  36. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37.  
  38. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. //            Component Dispatcher
  40. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41.  
  42. #define SoundComponentEntryPoint        main    
  43.  
  44. #include "ComponentDispatch.c"
  45.  
  46.  
  47. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. //            prototypes
  49. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50.  
  51. static void VUMeterBuffer(SoundComponentGlobalsPtr globals, const Byte * const inputBuffer, long samples, short sampleSize, short numChannels);
  52.  
  53.  
  54. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. // Component Manager Methods
  56. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  57. /*    ==============================================================================
  58.     Component Open
  59.  
  60.     This routine is called when the Component Manager creates an instance of this
  61.     component. The routine should allocate global variables in the appropriate heap
  62.     and call SetComponentInstanceStorage() so the Component Manager can remember
  63.     the globals and pass them to all the method calls.
  64.     
  65.     Determining the heap to use can be tricky. The Component Manager will normally
  66.     load the component code into the system heap, which is good, since many applications
  67.     will be sharing this component to play sound. In this case, the components's global
  68.     variable storage should also be created in the system heap.
  69.  
  70.     However, if system heap memory is tight, the Component Manager will load
  71.     the component into the application heap of the first application that plays sound.
  72.     When this happens, the component should create global storage in the application heap
  73.     instead. The Sound Manager will make sure that other applications will not try
  74.     to play sound while the component is in this application heap.
  75.  
  76.     To determine the proper heap to use, call GetComponentInstanceA5(). If the value
  77.     returned is 0, then the component was loaded into the system heap, and all storage
  78.     should be allocated there. If the value returned is non-zero, the component is in
  79.     the application heap specifed by returned A5 value, and all storage should be
  80.     allocated in this application heap.
  81.     
  82.     NOTE: If the component is loaded into the application heap, the value returned by
  83.     GetComponentRefCon() will be 0.
  84.     NOTE: Do not attempt to initialize in this call, since the Component Manager will
  85.     call Open() BEFORE calling Register().
  86.     NOTE: This routine is never called at interrupt time.
  87.     ============================================================================== */
  88.  
  89. static pascal ComponentResult __SoundComponentOpen(void *unused1, ComponentInstance self) {
  90. #pragma unused (unused1)
  91.  
  92.     Handle            h;
  93.     SoundComponentGlobalsPtr        globals;
  94.  
  95.     h = NewHandleClear(sizeof(SoundComponentGlobals));        // get space for globals
  96.     if (h == nil)
  97.         return(MemError());
  98.  
  99.     HLock(h);
  100.     globals = (SoundComponentGlobalsPtr) *h;
  101.     SetComponentInstanceStorage (self, (Handle) globals);     // save pointer to our globals
  102.  
  103.     globals->globalsHandle = h;                                // remember the handle
  104.  
  105.     return (noErr);
  106. }
  107.  
  108. /*    ==============================================================================
  109.     Component Close
  110.  
  111.     This routine is called when the Component Manager is closing the instance of
  112.     this component. It should delete all global storage and close any other components
  113.     that were opened.
  114.     
  115.     NOTE: Be sure to check that the globals pointer passed in to this routine is
  116.     not set to NIL. If the Open() routine fails for any reason, the Component
  117.     Manager will call this routine passing in a NIL for the globals.
  118.     NOTE: This routine is never called at interrupt time.
  119.     ============================================================================== */
  120.  
  121. static pascal ComponentResult __SoundComponentClose(SoundComponentGlobalsPtr globals, ComponentInstance self) {
  122. #pragma unused (self)
  123.  
  124.     if (globals) {                                            // we have some globals
  125.         if (globals->sourceComponent)                        // we have a source component
  126.             CloseComponent(globals->sourceComponent);        // close it
  127.  
  128.         DisposeHandle(globals->globalsHandle);                // dispose our storage
  129.     }
  130.  
  131.     return (noErr);
  132. }
  133.  
  134. /*    ==============================================================================
  135.     Component Register
  136.  
  137.     This routine is called once, usually at boot time, when the Component Manager
  138.     is first registering this sound component. This routine should check to see if the proper
  139.     Sound Manager is installed and return 0 if it is. If the right Sound Manager is not
  140.     installed, the routine should return 1 and this component will not be registered.
  141.  
  142.     NOTE: The cmpWantsRegisterMessage bit must be set in the component flags of the
  143.     sound component in order for this routine to be called.
  144.     NOTE: This routine is never called at interrupt time.
  145.     ============================================================================== */
  146.  
  147. static pascal ComponentResult __SoundComponentRegister(SoundComponentGlobalsPtr globals)
  148. {
  149. #pragma unused (globals)
  150.  
  151.     NumVersionVariant    version;
  152.  
  153.     version.parts = SndSoundManagerVersion();                    // get the Sound Manager version
  154.     if (version.whole < 0x03210000)                                // it's what we need
  155.         return (1);                                                // do not install component
  156.     else
  157.         return (0);                                                // install this component
  158. }
  159.  
  160. /*    ==============================================================================
  161.     Component GetInfo
  162.  
  163.     This is called when a program issues a SndGetInfo() call.  If we see our selector
  164.     we return the peak value of the last buffer, if it's anything else we forward
  165.     the selector to our source component.
  166.     ============================================================================== */
  167.  
  168. static pascal ComponentResult __SoundComponentGetInfo(SoundComponentGlobalsPtr globals, SoundSource sourceID, OSType selector, void *infoPtr)
  169. {
  170.     ComponentResult        result = noErr;
  171.  
  172.     switch (selector) {
  173.     
  174.         case kVUSelectorSubType:
  175.             ((short*)infoPtr)[0] = globals->peakSampleLeft;
  176.             ((short*)infoPtr)[1] = globals->peakSampleRight;
  177.             break;
  178.  
  179.         default:
  180.             result = SoundComponentGetInfo(globals->sourceComponent, sourceID, selector, infoPtr);
  181.             break;
  182.     }
  183.  
  184.     return (result);
  185. }
  186.  
  187. /*    ==============================================================================
  188.     StopSource
  189.  
  190.     This routine is used to stop sounds that are currently playing. It should
  191.     clear out any internal buffers, reset any compression state information
  192.     and then delegate the call up the chain.  We have no state so we just send
  193.     the call up the chain.
  194.  
  195.     NOTE: This can be called at interrupt time.
  196.     ============================================================================== */
  197.  
  198. static pascal ComponentResult __SoundComponentStopSource(SoundComponentGlobalsPtr globals, short count, SoundSource *sources) {
  199.  
  200.     // values are no longer valid
  201.     globals->peakSampleLeft = 0;
  202.     globals->peakSampleRight = 0;
  203.  
  204.     // delegate this call
  205.     return (SoundComponentStopSource(globals->sourceComponent, count, sources));
  206. }
  207.  
  208. /*    ==============================================================================
  209.     PlaySourceBuffer
  210.  
  211.     This routine is used to start a new sound playing. It should clear out any internal buffers
  212.     but should NOT reset any compression state information, since this could be a
  213.     continuation of a sound that has been broken into pieces. Then the call should be
  214.     delegated up the chain.  We have no state so we just send the call up the chain.
  215.  
  216.     NOTE: This can be called at interrupt time.
  217.     ============================================================================== */
  218.  
  219. static pascal ComponentResult __SoundComponentPlaySourceBuffer(SoundComponentGlobalsPtr globals, SoundSource sourceID, SoundParamBlockPtr pb, long actions) {
  220.  
  221.     // values are no longer valid, starting a new sound
  222.     globals->peakSampleLeft = 0;
  223.     globals->peakSampleRight = 0;
  224.  
  225.     // delegate this call
  226.     return (SoundComponentPlaySourceBuffer(globals->sourceComponent, sourceID, pb, actions));
  227. }
  228.  
  229. /*    ==============================================================================
  230.     SetSource
  231.  
  232.     This routine sets the component we should call to get more data. We must remember
  233.     this component.
  234.     ============================================================================== */
  235.  
  236. static pascal ComponentResult __SoundComponentSetSource(SoundComponentGlobalsPtr globals, SoundSource sourceID, ComponentInstance source) {
  237. #pragma unused (sourceID)
  238.  
  239.     // remember our source component
  240.     globals->sourceComponent = source;
  241.  
  242.     return (noErr);
  243. }
  244.  
  245. /*    ==============================================================================
  246.     GetSource
  247.  
  248.     This routine returns the component we call to get more data.
  249.     ============================================================================== */
  250.  
  251. static pascal ComponentResult __SoundComponentGetSource(SoundComponentGlobalsPtr globals, SoundSource sourceID, ComponentInstance *source) {
  252. #pragma unused (sourceID)
  253.  
  254.     *source = globals->sourceComponent;
  255.     return (noErr);
  256. }
  257.  
  258. /*    ==============================================================================
  259.     SetOutput
  260.  
  261.     This routine sets the data format our component should output. If we can't output
  262.     the requested format, we should return a pointer to the format we do support,
  263.     and return an error, and the Sound Manager will attempt the conversion for us.
  264.  
  265.     For this component, because we don't modify the data there is no problem
  266.     outputting any format.
  267.     ============================================================================== */
  268.  
  269. static pascal ComponentResult __SoundComponentSetOutput(SoundComponentGlobalsPtr globals, SoundComponentDataPtr requested, SoundComponentDataPtr *actual) {
  270. #pragma unused (actual, globals, requested)
  271.  
  272.     // no problem outputting anything because we only "look" at the data
  273.     return (noErr);
  274. }
  275.  
  276. /*    ==============================================================================
  277.     GetSourceData
  278.  
  279.     This routine is called when the Sound Manager wants your component to process
  280.     some more data.
  281.  
  282.     NOTE: This will most often be called at interrupt time.
  283.     ============================================================================== */
  284.  
  285. static pascal ComponentResult __SoundComponentGetSourceData(SoundComponentGlobalsPtr globals, SoundComponentDataPtr *resultDataPtr) {
  286. #pragma unused (resultDataPtr)
  287.     ComponentResult            result;
  288.  
  289.     // Get some sound data to look at
  290.     result = SoundComponentGetSourceData(globals->sourceComponent, resultDataPtr);
  291.  
  292.     // Sample the buffer to get VU meter data
  293.     VUMeterBuffer(globals, (*resultDataPtr)->buffer, (*resultDataPtr)->sampleCount, (*resultDataPtr)->sampleSize, (*resultDataPtr)->numChannels);
  294.  
  295.     return result;
  296. }
  297.  
  298. /*    ==============================================================================
  299.     VUMeterBuffer
  300.  
  301.     This implements a simple peak meter.
  302.  
  303.     This code works on the assumtion that the average of a sound wave is 0 (for
  304.     each positive sample there is a coresponding negative sample), therefore we
  305.     only have to find the largest positive sample in the buffer because that will
  306.     be very close to the absolute value of the largest negative sample.
  307.     ============================================================================== */
  308.  
  309. static void VUMeterBuffer(SoundComponentGlobalsPtr globals, const Byte * const inputBuffer, long samples, short sampleSize, short numChannels) {
  310.     int        i;
  311.     short    *shortBuffer;
  312.     Byte    *charBuffer;
  313.  
  314.     shortBuffer = (short*)inputBuffer;
  315.     charBuffer = (Byte*)inputBuffer;
  316.  
  317.     globals->peakSampleLeft = 0;
  318.     globals->peakSampleRight = 0;
  319.  
  320.     if (sampleSize == 8) {
  321.         if (numChannels == 1) {
  322.             for (i = 0; i < samples; i++) {
  323.                 if (*charBuffer > globals->peakSampleLeft)
  324.                     globals->peakSampleLeft = *charBuffer;
  325.                 charBuffer++;
  326.             }
  327.         } else if (numChannels == 2) {
  328.             for (i = 0; i < samples * 2; i++) {
  329.                 if (*charBuffer > globals->peakSampleLeft)
  330.                     globals->peakSampleLeft = *charBuffer;
  331.                 charBuffer++;
  332.                 if (*charBuffer > globals->peakSampleRight)
  333.                     globals->peakSampleRight = *charBuffer;
  334.                 charBuffer++;
  335.             }
  336.         }
  337.     } else if (sampleSize == 16) {
  338.         if (numChannels == 1) {
  339.             for (i = 0; i < samples; i++) {
  340.                 if (*shortBuffer > globals->peakSampleLeft)
  341.                     globals->peakSampleLeft = *shortBuffer;
  342.                 shortBuffer++;
  343.             }
  344.         } else if (numChannels == 2) {
  345.             for (i = 0; i < samples * 2; i++) {
  346.                 if (*shortBuffer > globals->peakSampleLeft)
  347.                     globals->peakSampleLeft = *shortBuffer;
  348.                 shortBuffer++;
  349.                 if (*shortBuffer > globals->peakSampleRight)
  350.                     globals->peakSampleRight = *shortBuffer;
  351.                 shortBuffer++;
  352.             }
  353.         }
  354.     }
  355. }
  356.  
  357.